﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace jQM
{
    public static partial class HtmlHelperListExtensionMethods
    {
        private static string buildList(IEnumerable<BasicListItem> list)
        {
            StringBuilder html = new StringBuilder();

            foreach (var item in list)
            {
                html.Append("<li>");
                html.Append(item.Text);
                html.Append("</li>");
                html.AppendLine();
            }

            return html.ToString();
        }

        private static string buildLinkedList(IEnumerable<BasicLinkedListItem> list)
        {
            StringBuilder html = new StringBuilder();

            foreach (var item in list)
            {
                html.Append("<li>");
                if (item.IsExternal)
                {
                    html.Append(string.Format("<a href=\"{0}\" rel=\"{1}\">", item.Link, "external"));
                }
                else
                {
                    html.Append(string.Format("<a href=\"{0}\">", item.Link));
                }
                html.Append(item.Text);
                html.Append("</a>");
                html.Append("</li>");
                html.AppendLine();
            }

            return html.ToString();
        }

        private static string buildGroupedList(IEnumerable<Group<BasicListItem>> groupedList)
        {
            StringBuilder html = new StringBuilder();
            foreach (Group<BasicListItem> group in groupedList)
            {
                html.Append(string.Format("<li data-role=\"{0}\">", "list-divider"));
                html.Append(group.Header);
                html.Append("</li>");
                html.Append(buildList(group.Items));
            }

            return html.ToString();
        }

        private static string buildGroupedLinkedList(IEnumerable<Group<BasicLinkedListItem>> groupedList)
        {
            StringBuilder html = new StringBuilder();
            foreach (Group<BasicLinkedListItem> group in groupedList)
            {
                html.Append(string.Format("<li data-role=\"{0}\">", "list-divider"));
                html.Append(group.Header);
                html.Append("</li>");
                html.Append(buildLinkedList(group.Items));
            }

            return html.ToString();
        }
    }
}